<!DOCTYPE html>
<html lang="en">
	<head>
		<meta charset="UTF-8" />
		<meta http-equiv="X-UA-Compatible" content="IE=edge" />
		<meta name="viewport" content="width=device-width, initial-scale=1.0" />
		<title>下拉加载</title>
		<script src="https://unpkg.com/vue@next"></script>
		<style>
			.body {
				overflow-y: hidden;
			}
			.content {
				width: 700px;
				height: 850px;
				border: 1px solid red;
				padding: 10px 15px;
				margin: 0 auto;
				overflow-y: scroll;
			}
			.box-list {
				width: 100%;
			}
			.box-item {
				width: 100%;
				border: 1px solid green;
				border-radius: 4px;
				margin-bottom: 20px;
			}
			.box-img {
				height: 150px;
				overflow: hidden;
			}
			.box-img img {
				width: 100%;
			}
			.box-info {
				padding: 0 20px;
			}
			.hint-info {
				text-align: center;
				color: #ccc;
			}
		</style>
	</head>
	<body>
		<div id="root">
			<div class="content" @scroll="checkScroll">
				<div class="box-list">
					<div
						class="box-item"
						v-for="(item,index) in picList_show"
						:key="item"
					>
						<div class="box-img">
							<img :src=`./images/pic/${index+1}.webp` alt="1" />
						</div>
						<div class="box-info">
							<p>{{item}}</p>
						</div>
					</div>
				</div>
				<div class="hint-info">{{hint_text}}</div>
			</div>
		</div>
		<script>
			const app = Vue.createApp({
				data() {
					return {
						picList: [
							"第一张图片",
							"第二张图片",
							"第三张图片",
							"第四张图片",
							"第五张图片",
							"第六张图片",
							"第七张图片",
							"第八张图片",
							"第九张图片",
							"第十张图片",
						],
						picList_show: [],
						hint_text: "",
						lock: true,
					}
				},
				created() {
					this.picList_show = this.picList.slice(0, 4)
				},
				methods: {
					checkListLength() {
						//判断picList_show是否已经加载全部图片
						return this.picList.length === this.picList_show.length
					},
					checkScroll(e) {
						this.lock = false
						const check = this.checkScrollBottom(e.srcElement)
						if (check) {
							//如果达到底部，则判断数组
							if (this.checkListLength()) {
								this.hint_text = "已经到底了哦"
							} else {
								//达到底部，但数组还未加载完
								this.hint_text = "加载中..."
								setTimeout(() => {
									this.lock = true
									const length = this.picList_show.length
									this.picList_show = this.picList_show.concat(
										this.picList.slice(length, length + 1)
									)
								}, 300)
							}
						}
					},
					checkScrollBottom(o) {
						//判断滚动条是否已经滚动到底部
						/* console.log("scrollTop:", o.scrollTop)
						console.log("offsetHeight:", o.offsetHeight)
						console.log("scrollHeight:", o.scrollHeight) */
						//clientHeight或offsetHeight（包含padding border）
						return o.scrollTop + o.clientHeight >= o.scrollHeight
					},
				},
			})
			const vm = app.mount("#root")
		</script>
	</body>
</html>
